
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/serving/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:08 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Serving WSGI Applications &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../genindex/index.html" />
    <link rel="search" title="Search" href="../search/index.html" />
    <link rel="next" title="Test Utilities" href="../test/index.html" />
    <link rel="prev" title="Quickstart" href="../quickstart/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "serving"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../test/index.html" title="Test Utilities"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../quickstart/index.html" title="Quickstart"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-werkzeug.serving">
<span id="serving-wsgi-applications"></span><h1>Serving WSGI Applications<a class="headerlink" href="#module-werkzeug.serving" title="Permalink to this headline">¶</a></h1>
<p>There are many ways to serve a WSGI application.  While you’re developing it,
you usually don’t want to have a full-blown webserver like Apache up and
running, but instead a simple standalone one.  Because of that Werkzeug comes
with a builtin development server.</p>
<p>The easiest way is creating a small <code class="docutils literal notranslate"><span class="pre">start-myproject.py</span></code> file that runs the
application using the builtin server:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>

<span class="kn">from</span> <span class="nn">werkzeug.serving</span> <span class="k">import</span> <span class="n">run_simple</span>
<span class="kn">from</span> <span class="nn">myproject</span> <span class="k">import</span> <span class="n">make_app</span>

<span class="n">app</span> <span class="o">=</span> <span class="n">make_app</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">run_simple</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">,</span> <span class="n">app</span><span class="p">,</span> <span class="n">use_reloader</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also pass it the <cite>extra_files</cite> keyword argument with a list of
additional files (like configuration files) you want to observe.</p>
<dl class="function">
<dt id="werkzeug.serving.run_simple">
<code class="descclassname">werkzeug.serving.</code><code class="descname">run_simple</code><span class="sig-paren">(</span><em>hostname</em>, <em>port</em>, <em>application</em>, <em>use_reloader=False</em>, <em>use_debugger=False</em>, <em>use_evalex=True</em>, <em>extra_files=None</em>, <em>reloader_interval=1</em>, <em>reloader_type='auto'</em>, <em>threaded=False</em>, <em>processes=1</em>, <em>request_handler=None</em>, <em>static_files=None</em>, <em>passthrough_errors=False</em>, <em>ssl_context=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.serving.run_simple" title="Permalink to this definition">¶</a></dt>
<dd><p>Start a WSGI application. Optional features include a reloader,
multithreading and fork support.</p>
<p>This function has a command-line interface too:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">werkzeug</span><span class="o">.</span><span class="n">serving</span> <span class="o">--</span><span class="n">help</span>
</pre></div>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.15: </span>Bind to a Unix socket by passing a path that starts with
<code class="docutils literal notranslate"><span class="pre">unix://</span></code> as the <code class="docutils literal notranslate"><span class="pre">hostname</span></code>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.10: </span>Improved the reloader and added support for changing the backend
through the <cite>reloader_type</cite> parameter.  See <a class="reference internal" href="#reloader"><span class="std std-ref">Reloader</span></a>
for more information.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9: </span>Added command-line interface.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.8: </span>Added support for automatically loading a SSL context from certificate
file and private key.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>support for SSL was added.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5: </span><cite>static_files</cite> was added to simplify serving of static files as well
as <cite>passthrough_errors</cite>.</p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>hostname</strong> – The host to bind to, for example <code class="docutils literal notranslate"><span class="pre">'localhost'</span></code>.
If the value is a path that starts with <code class="docutils literal notranslate"><span class="pre">unix://</span></code> it will bind
to a Unix socket instead of a TCP socket..</li>
<li><strong>port</strong> – The port for the server.  eg: <code class="docutils literal notranslate"><span class="pre">8080</span></code></li>
<li><strong>application</strong> – the WSGI application to execute</li>
<li><strong>use_reloader</strong> – should the server automatically restart the python
process if modules were changed?</li>
<li><strong>use_debugger</strong> – should the werkzeug debugging system be used?</li>
<li><strong>use_evalex</strong> – should the exception evaluation feature be enabled?</li>
<li><strong>extra_files</strong> – a list of files the reloader should watch
additionally to the modules.  For example configuration
files.</li>
<li><strong>reloader_interval</strong> – the interval for the reloader in seconds.</li>
<li><strong>reloader_type</strong> – the type of reloader to use.  The default is
auto detection.  Valid values are <code class="docutils literal notranslate"><span class="pre">'stat'</span></code> and
<code class="docutils literal notranslate"><span class="pre">'watchdog'</span></code>. See <a class="reference internal" href="#reloader"><span class="std std-ref">Reloader</span></a> for more
information.</li>
<li><strong>threaded</strong> – should the process handle each request in a separate
thread?</li>
<li><strong>processes</strong> – if greater than 1 then handle each request in a new process
up to this maximum number of concurrent processes.</li>
<li><strong>request_handler</strong> – optional parameter that can be used to replace
the default one.  You can use this to replace it
with a different
<code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code>
subclass.</li>
<li><strong>static_files</strong> – a list or dict of paths for static files.  This works
exactly like <code class="xref py py-class docutils literal notranslate"><span class="pre">SharedDataMiddleware</span></code>, it’s actually
just wrapping the application in that middleware before
serving.</li>
<li><strong>passthrough_errors</strong> – set this to <cite>True</cite> to disable the error catching.
This means that the server will die on errors but
it can be useful to hook debuggers in (pdb etc.)</li>
<li><strong>ssl_context</strong> – an SSL context for the connection. Either an
<a class="reference external" href="https://docs.python.org/3/library/ssl.html#ssl.SSLContext" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a>, a tuple in the form
<code class="docutils literal notranslate"><span class="pre">(cert_file,</span> <span class="pre">pkey_file)</span></code>, the string <code class="docutils literal notranslate"><span class="pre">'adhoc'</span></code> if
the server should automatically create one, or <code class="docutils literal notranslate"><span class="pre">None</span></code>
to disable SSL (which is the default).</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="werkzeug.serving.is_running_from_reloader">
<code class="descclassname">werkzeug.serving.</code><code class="descname">is_running_from_reloader</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.serving.is_running_from_reloader" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks if the application is running from within the Werkzeug
reloader subprocess.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.10.</span></p>
</div>
</details></dd></dl>

<dl class="function">
<dt id="werkzeug.serving.make_ssl_devcert">
<code class="descclassname">werkzeug.serving.</code><code class="descname">make_ssl_devcert</code><span class="sig-paren">(</span><em>base_path</em>, <em>host=None</em>, <em>cn=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.serving.make_ssl_devcert" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an SSL key for development.  This should be used instead of
the <code class="docutils literal notranslate"><span class="pre">'adhoc'</span></code> key which generates a new cert on each server start.
It accepts a path for where it should store the key and cert and
either a host or CN.  If a host is given it will use the CN
<code class="docutils literal notranslate"><span class="pre">*.host/CN=host</span></code>.</p>
<p>For more information see <a class="reference internal" href="#werkzeug.serving.run_simple" title="werkzeug.serving.run_simple"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_simple()</span></code></a>.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>base_path</strong> – the path to the certificate and key.  The extension
<code class="docutils literal notranslate"><span class="pre">.crt</span></code> is added for the certificate, <code class="docutils literal notranslate"><span class="pre">.key</span></code> is
added for the key.</li>
<li><strong>host</strong> – the name of the host.  This can be used as an alternative
for the <cite>cn</cite>.</li>
<li><strong>cn</strong> – the <cite>CN</cite> to use.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<div class="admonition-information admonition">
<p class="first admonition-title">Information</p>
<p class="last">The development server is not intended to be used on production systems.
It was designed especially for development purposes and performs poorly
under high load.  For deployment setups have a look at the
<a class="reference internal" href="../deployment/index.html#deployment"><span class="std std-ref">Application Deployment</span></a> pages.</p>
</div>
<div class="section" id="reloader">
<span id="id1"></span><h2>Reloader<a class="headerlink" href="#reloader" title="Permalink to this headline">¶</a></h2>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.10.</span></p>
</div>
</details><p>The Werkzeug reloader constantly monitors modules and paths of your web
application, and restarts the server if any of the observed files change.</p>
<p>Since version 0.10, there are two backends the reloader supports: <code class="docutils literal notranslate"><span class="pre">stat</span></code> and
<code class="docutils literal notranslate"><span class="pre">watchdog</span></code>.</p>
<ul class="simple">
<li>The default <code class="docutils literal notranslate"><span class="pre">stat</span></code> backend simply checks the <code class="docutils literal notranslate"><span class="pre">mtime</span></code> of all files in a
regular interval. This is sufficient for most cases, however, it is known to
drain a laptop’s battery.</li>
<li>The <code class="docutils literal notranslate"><span class="pre">watchdog</span></code> backend uses filesystem events, and is much faster than
<code class="docutils literal notranslate"><span class="pre">stat</span></code>. It requires the <a class="reference external" href="https://pypi.org/project/watchdog/">watchdog</a>
module to be installed. The recommended way to achieve this is to add
<code class="docutils literal notranslate"><span class="pre">Werkzeug[watchdog]</span></code> to your requirements file.</li>
</ul>
<p>If <code class="docutils literal notranslate"><span class="pre">watchdog</span></code> is installed and available it will automatically be used
instead of the builtin <code class="docutils literal notranslate"><span class="pre">stat</span></code> reloader.</p>
<p>To switch between the backends you can use the <cite>reloader_type</cite> parameter of the
<a class="reference internal" href="#werkzeug.serving.run_simple" title="werkzeug.serving.run_simple"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_simple()</span></code></a> function. <code class="docutils literal notranslate"><span class="pre">'stat'</span></code> sets it to the default stat based
polling and <code class="docutils literal notranslate"><span class="pre">'watchdog'</span></code> forces it to the watchdog backend.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Some edge cases, like modules that failed to import correctly, are not
handled by the stat reloader for performance reasons. The watchdog reloader
monitors such files too.</p>
</div>
</div>
<div class="section" id="colored-logging">
<h2>Colored Logging<a class="headerlink" href="#colored-logging" title="Permalink to this headline">¶</a></h2>
<p>The development server can optionally highlight the request logs in
different colors based on the status code. Install <a class="reference external" href="https://pypi.org/project/click/">Click</a> to enable
this feature.</p>
</div>
<div class="section" id="virtual-hosts">
<h2>Virtual Hosts<a class="headerlink" href="#virtual-hosts" title="Permalink to this headline">¶</a></h2>
<p>Many web applications utilize multiple subdomains.  This can be a bit tricky
to simulate locally.  Fortunately there is the <a class="reference external" href="https://en.wikipedia.org/wiki/Hosts_file">hosts file</a> that can be used
to assign the local computer multiple names.</p>
<p>This allows you to call your local computer <cite>yourapplication.local</cite> and
<cite>api.yourapplication.local</cite> (or anything else) in addition to <cite>localhost</cite>.</p>
<p>You can find the hosts file on the following location:</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="25%" />
<col width="75%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Windows</td>
<td><code class="docutils literal notranslate"><span class="pre">%SystemRoot%\system32\drivers\etc\hosts</span></code></td>
</tr>
<tr class="row-even"><td>Linux / OS X</td>
<td><code class="docutils literal notranslate"><span class="pre">/etc/hosts</span></code></td>
</tr>
</tbody>
</table>
</div></blockquote>
<p>You can open the file with your favorite text editor and add a new name after
<cite>localhost</cite>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span>       <span class="n">localhost</span> <span class="n">yourapplication</span><span class="o">.</span><span class="n">local</span> <span class="n">api</span><span class="o">.</span><span class="n">yourapplication</span><span class="o">.</span><span class="n">local</span>
</pre></div>
</div>
<p>Save the changes and after a while you should be able to access the
development server on these host names as well.  You can use the
<a class="reference internal" href="../routing/index.html#routing"><span class="std std-ref">URL Routing</span></a> system to dispatch between different hosts or parse
<code class="xref py py-attr docutils literal notranslate"><span class="pre">request.host</span></code> yourself.</p>
</div>
<div class="section" id="shutting-down-the-server">
<h2>Shutting Down The Server<a class="headerlink" href="#shutting-down-the-server" title="Permalink to this headline">¶</a></h2>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
</details><p>Starting with Werkzeug 0.7 the development server provides a way to shut
down the server after a request.  This currently only works with Python
2.6 and later and will only work with the development server.  To initiate
the shutdown you have to call a function named
<code class="docutils literal notranslate"><span class="pre">'werkzeug.server.shutdown'</span></code> in the WSGI environment:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">shutdown_server</span><span class="p">(</span><span class="n">environ</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;werkzeug.server.shutdown&#39;</span> <span class="ow">in</span> <span class="n">environ</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Not running the development server&#39;</span><span class="p">)</span>
    <span class="n">environ</span><span class="p">[</span><span class="s1">&#39;werkzeug.server.shutdown&#39;</span><span class="p">]()</span>
</pre></div>
</div>
</div>
<div class="section" id="troubleshooting">
<h2>Troubleshooting<a class="headerlink" href="#troubleshooting" title="Permalink to this headline">¶</a></h2>
<p>On operating systems that support ipv6 and have it configured such as modern
Linux systems, OS X 10.4 or higher as well as Windows Vista some browsers can
be painfully slow if accessing your local server.  The reason for this is that
sometimes “localhost” is configured to be available on both ipv4 and ipv6 sockets
and some browsers will try to access ipv6 first and then ipv4.</p>
<p>At the current time the integrated webserver does not support ipv6 and ipv4 at
the same time and for better portability ipv4 is the default.</p>
<p>If you notice that the web browser takes ages to load the page there are two ways
around this issue.  If you don’t need ipv6 support you can disable the ipv6 entry
in the <a class="reference external" href="https://en.wikipedia.org/wiki/Hosts_file">hosts file</a> by removing this line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">::</span><span class="mi">1</span>             <span class="n">localhost</span>
</pre></div>
</div>
<p>Alternatively you can also disable ipv6 support in your browser.  For example
if Firefox shows this behavior you can disable it by going to <code class="docutils literal notranslate"><span class="pre">about:config</span></code>
and disabling the <cite>network.dns.disableIPv6</cite> key.  This however is not
recommended as of Werkzeug 0.6.1!</p>
<p>Starting with Werkzeug 0.6.1, the server will now switch between ipv4 and
ipv6 based on your operating system’s configuration.  This means if that
you disabled ipv6 support in your browser but your operating system is
preferring ipv6, you will be unable to connect to your server.  In that
situation, you can either remove the localhost entry for <code class="docutils literal notranslate"><span class="pre">::1</span></code> or
explicitly bind the hostname to an ipv4 address (<cite>127.0.0.1</cite>)</p>
</div>
<div class="section" id="ssl">
<h2>SSL<a class="headerlink" href="#ssl" title="Permalink to this headline">¶</a></h2>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.6.</span></p>
</div>
</details><p>The builtin server supports SSL for testing purposes.  If an SSL context is
provided it will be used.  That means a server can either run in HTTP or HTTPS
mode, but not both.</p>
<div class="section" id="quickstart">
<h3>Quickstart<a class="headerlink" href="#quickstart" title="Permalink to this headline">¶</a></h3>
<p>The easiest way to do SSL based development with Werkzeug is by using it
to generate an SSL certificate and private key and storing that somewhere
and to then put it there.  For the certificate you need to provide the
name of your server on generation or a <cite>CN</cite>.</p>
<ol class="arabic">
<li><p class="first">Generate an SSL key and store it somewhere:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">werkzeug.serving</span> <span class="k">import</span> <span class="n">make_ssl_devcert</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">make_ssl_devcert</span><span class="p">(</span><span class="s1">&#39;/path/to/the/key&#39;</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s1">&#39;localhost&#39;</span><span class="p">)</span>
<span class="go">(&#39;/path/to/the/key.crt&#39;, &#39;/path/to/the/key.key&#39;)</span>
</pre></div>
</div>
</li>
<li><p class="first">Now this tuple can be passed as <code class="docutils literal notranslate"><span class="pre">ssl_context</span></code> to the
<a class="reference internal" href="#werkzeug.serving.run_simple" title="werkzeug.serving.run_simple"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_simple()</span></code></a> method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">run_simple</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">4000</span><span class="p">,</span> <span class="n">application</span><span class="p">,</span>
           <span class="n">ssl_context</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;/path/to/the/key.crt&#39;</span><span class="p">,</span>
                        <span class="s1">&#39;/path/to/the/key.key&#39;</span><span class="p">))</span>
</pre></div>
</div>
</li>
</ol>
<p>You will have to acknowledge the certificate in your browser once then.</p>
</div>
<div class="section" id="loading-contexts-by-hand">
<h3>Loading Contexts by Hand<a class="headerlink" href="#loading-contexts-by-hand" title="Permalink to this headline">¶</a></h3>
<p>In Python 2.7.9 and 3+ you also have the option to use a <code class="docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code>
object instead of a simple tuple. This way you have better control over the SSL
behavior of Werkzeug’s builtin server:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">ssl</span>
<span class="n">ctx</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLContext</span><span class="p">(</span><span class="n">ssl</span><span class="o">.</span><span class="n">PROTOCOL_SSLv23</span><span class="p">)</span>
<span class="n">ctx</span><span class="o">.</span><span class="n">load_cert_chain</span><span class="p">(</span><span class="s1">&#39;ssl.cert&#39;</span><span class="p">,</span> <span class="s1">&#39;ssl.key&#39;</span><span class="p">)</span>
<span class="n">run_simple</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">4000</span><span class="p">,</span> <span class="n">application</span><span class="p">,</span> <span class="n">ssl_context</span><span class="o">=</span><span class="n">ctx</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="generating-certificates">
<h3>Generating Certificates<a class="headerlink" href="#generating-certificates" title="Permalink to this headline">¶</a></h3>
<p>A key and certificate can be created in advance using the openssl tool
instead of the <a class="reference internal" href="#werkzeug.serving.make_ssl_devcert" title="werkzeug.serving.make_ssl_devcert"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_ssl_devcert()</span></code></a>.  This requires that you have
the <cite>openssl</cite> command installed on your system:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ openssl genrsa 1024 &gt; ssl.key
$ openssl req -new -x509 -nodes -sha1 -days 365 -key ssl.key &gt; ssl.cert
</pre></div>
</div>
</div>
<div class="section" id="adhoc-certificates">
<h3>Adhoc Certificates<a class="headerlink" href="#adhoc-certificates" title="Permalink to this headline">¶</a></h3>
<p>The easiest way to enable SSL is to start the server in adhoc-mode.  In
that case Werkzeug will generate an SSL certificate for you:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">run_simple</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">4000</span><span class="p">,</span> <span class="n">application</span><span class="p">,</span>
           <span class="n">ssl_context</span><span class="o">=</span><span class="s1">&#39;adhoc&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The downside of this of course is that you will have to acknowledge the
certificate each time the server is reloaded.  Adhoc certificates are
discouraged because modern browsers do a bad job at supporting them for
security reasons.</p>
<p>This feature requires the cryptography library to be installed.</p>
</div>
</div>
<div class="section" id="unix-sockets">
<h2>Unix Sockets<a class="headerlink" href="#unix-sockets" title="Permalink to this headline">¶</a></h2>
<p>The dev server can bind to a Unix socket instead of a TCP socket.
<a class="reference internal" href="#werkzeug.serving.run_simple" title="werkzeug.serving.run_simple"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_simple()</span></code></a> will bind to a Unix socket if the <code class="docutils literal notranslate"><span class="pre">hostname</span></code>
parameter starts with <code class="docutils literal notranslate"><span class="pre">'unix://'</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.serving</span> <span class="k">import</span> <span class="n">run_simple</span>
<span class="n">run_simple</span><span class="p">(</span><span class="s1">&#39;unix://example.sock&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  

  <h3>Contents</h3>
  <ul>
<li><a class="reference internal" href="#">Serving WSGI Applications</a><ul>
<li><a class="reference internal" href="#reloader">Reloader</a></li>
<li><a class="reference internal" href="#colored-logging">Colored Logging</a></li>
<li><a class="reference internal" href="#virtual-hosts">Virtual Hosts</a></li>
<li><a class="reference internal" href="#shutting-down-the-server">Shutting Down The Server</a></li>
<li><a class="reference internal" href="#troubleshooting">Troubleshooting</a></li>
<li><a class="reference internal" href="#ssl">SSL</a><ul>
<li><a class="reference internal" href="#quickstart">Quickstart</a></li>
<li><a class="reference internal" href="#loading-contexts-by-hand">Loading Contexts by Hand</a></li>
<li><a class="reference internal" href="#generating-certificates">Generating Certificates</a></li>
<li><a class="reference internal" href="#adhoc-certificates">Adhoc Certificates</a></li>
</ul>
</li>
<li><a class="reference internal" href="#unix-sockets">Unix Sockets</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
  <li><a href="../index.html">Overview</a>
    <ul>
          <li>Previous: <a href="../quickstart/index.html" title="previous chapter">Quickstart</a>
          <li>Next: <a href="../test/index.html" title="next chapter">Test Utilities</a>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/serving/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:08 GMT -->
</html>